package administration.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import javax.swing.text.DefaultEditorKit.InsertContentAction;

import model.DatabaseException;
import model.User;
import model.Utilities;
/**
 * Servlet implementation class DefineColors
 */
@WebServlet("/DefineColors")
public class DefineColors extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null || !user.getAccountType().equals("A")){
			response.sendRedirect("Login.html");
			return;
		}
		request.setCharacterEncoding("UTF-8");
		Map<String,String[]> parameterMap = request.getParameterMap();
		String[] colorNames = parameterMap.get("colorNames[]");
		String[] colorCodes = parameterMap.get("colorCodes[]");
		try {
			Utilities.insertColors(colorNames, colorCodes);
			response.sendRedirect("/Store/Administration.html");
		} catch (DatabaseException e) {
			e.printStackTrace();
			response.sendRedirect("/Store/TechnicalDifficulties.html");
		}
	}

}
